package root.electronclass.electronclass.personal.activity

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import root.electronclass.electronclass.R
import root.electronclass.electronclass.databinding.ItemGrowth1Binding
import root.electronclass.electronclass.databinding.ItemGrowthBinding
import root.electronclass.electronclass.mvp.model.GrowthModel

/**
 *@ClassName TODO
 *@Description TODO
 *@Author caofc
 *@Date 2023/11/27 00:00
 */
class GrowthAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private val type1 = 0
    private val type2 = 1
    private val list: MutableList<GrowthModel> = mutableListOf()

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return when (viewType) {
            type1 -> {
                val view =ItemGrowthBinding.inflate(LayoutInflater.from(parent.context), parent, false)
                Holder1(view, list.size)
            }
            else  -> {
                val view = ItemGrowth1Binding.inflate(LayoutInflater.from(parent.context),parent, false)
                Holder2(view, list.size)
            }
        }
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        when (holder) {
            is Holder1 -> {
                holder.bind(list[position])
            }
            is Holder2 -> {
                holder.bind(list[position])
            }
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (position % 2 == 0) type1 else type2
    }

   open fun refresh(list: MutableList<GrowthModel>) {
        this.list.clear()
        this.list.addAll(list)
        this.notifyDataSetChanged()
    }
}



class Holder1(val view: ItemGrowthBinding, val size: Int) : RecyclerView.ViewHolder(view.root) {
    fun bind(t: GrowthModel) {
        view.num1.text = (adapterPosition + 1).toString()
        view.name.text = t.name
        view.xing1.setBackgroundResource(if (t.status == 1) R.mipmap.icon_xingxing_true else R.mipmap.icon_xingxing_false)
        view.lineLeft.isVisible = adapterPosition != size - 1
    }
}

class Holder2(val view: ItemGrowth1Binding, val size: Int) : RecyclerView.ViewHolder(view.root) {
    fun bind(t: GrowthModel) {
        view.num2.text = (adapterPosition + 1).toString()
        view.name.text = t.name
        view.xing2.setBackgroundResource(if (t.status == 1) R.mipmap.icon_xingxing_true else R.mipmap.icon_xingxing_false)
        //当前是最后一个
        view.lineRight.isVisible = adapterPosition != size - 1
    }
}